package org.hp.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

@WebFilter(urlPatterns = "*.html")
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request= (HttpServletRequest) req;
        String url=request.getRequestURI();
        System.out.println("url="+url);
        Map map= (Map) request.getSession().getAttribute("map");
        System.out.println(map);
        if(map!=null||url.indexOf("UserServlet")!=-1||url.indexOf("index")!=-1){
            System.out.println("放行方法");
            chain.doFilter(req, resp);
        }else{
            System.out.println("拦截方法");
            HttpServletResponse response=(HttpServletResponse) resp;
            response.sendRedirect("/index.html");
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
